package {

  import flash.display.Sprite;
  import flash.filters.BevelFilter;

  public class Arc extends Sprite
  {
    private var color:uint;
    private var radius:Number;
    private var angle:Number;
    private var startAngle:Number;

    public function Arc(color:uint, x:Number, y:Number, radius:Number, angle:Number = 0, startAngle:Number = 0)
    {
      this.color = color;
      this.x = x;
      this.y = y;
      this.radius = radius;
      this.angle = angle > 360 ? Math.PI * 2 : Math.PI/180 * angle;
      this.startAngle = Math.PI/180 * startAngle;

      drawArc();
       
    }

    public function get centerX():Number {
      return Math.cos(startAngle + angle/2) * radius * 1;
    }

    public function get centerY():Number {
      return Math.sin(startAngle + angle/2) * radius * 1;
    }

    private function drawArc():void  {
      graphics.clear();
      graphics.beginFill(color);
      graphics.lineStyle(1, 0xaaaaaa);

      var delta:Number = angle/8;

      var ctrlDist:Number = radius/Math.cos(delta/2);

      var currAngle:Number = startAngle;
      var ctrlX:Number;
      var ctrlY:Number;
      var anchorX:Number;
      var anchorY:Number;

      var startX:Number = x + Math.cos(startAngle) * radius;
      var startY:Number = y + Math.sin(startAngle) * radius;

      graphics.moveTo(x, y);
      graphics.lineTo(startX, startY);

      // Repeat eight times to create eight segments.
      for (var i:Number = 0; i < 8; i++) {
        currAngle += delta;

        // The control points are derived using sine and cosine.
        ctrlX = x + Math.cos(currAngle-(delta/2))*(ctrlDist);
        ctrlY = y + Math.sin(currAngle-(delta/2))*(ctrlDist);

        // The anchor points (end points of the curve) can be found similarly to the
        // control points.
        anchorX = x + Math.cos(currAngle) * radius;
        anchorY = y + Math.sin(currAngle) * radius;

        // Draw the segment.
        graphics.curveTo(ctrlX, ctrlY, anchorX, anchorY);
      }
      graphics.lineTo(x, y);

      graphics.endFill();

      var bevel:BevelFilter = new BevelFilter();
      bevel.shadowColor = 0x777777;
      bevel.distance=1;
      bevel.blurX=0;
      bevel.blurY=0;
      bevel.strength=1;
      this.filters = [ bevel ];
  }

  }

}
 
